﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WoodAdmin.Models;
using WoodAdmin.Models.ViewModels;
using WoodAdmin.Services;
using System;
using System.Linq;
using X.PagedList;

namespace WoodAdmin.Controllers
{
    public class SysNodeController : BaseController
    {
        private readonly SysNodeService _service;
        private readonly SysUserService _user;
        public SysNodeController(SysNodeService service, SysUserService user)
        {
            _service = service;
            _user = user;
        }

        #region 列表
        /// <summary>
        /// 节点数首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取节点树
        /// </summary>
        /// <returns></returns>
        public IActionResult NodeStructure()
        {
            var a = _service.Queryable().Select(p => new { id = p.ID, title = p.Title, parentId = p.Pid, checkArr = 0});
            return Json(new DTreeModel { status = new Status { code = 200, message = "操作成功" }, data = a });
        }

        /// <summary>
        /// 获取单条节点信息
        /// </summary>
        /// <returns></returns>
        public IActionResult NodeInfo(int id)
        {
           return Json( _service.FindEntity(id));
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 编辑/添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddEdit(int id)
        {
            SysNode model = _service.FindEntity(id);
            if (model==null)
            {
                model = new SysNode();
            }
            return View(model);
        }

        /// <summary>
        /// 编辑/添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddEdit(SysNode model,string type)
        {
            SysUser user = CurrentUser;
            if (type=="1")
            {
                model.CreateTime = DateTime.Now;
                model.CreateMan = user.ID;
                model.Pid = model.ID.ToString();
                if (_service.Insert(model))
                {
                    return JsonSuccess("成功");
                }
                return JsonError("失败");
            }
            if (type=="2")
            {
                model.ModifyTIme = DateTime.Now;
                model.ModifyMan = user.ID; 
                if (_service.Update(model))
                {
                    return JsonSuccess("成功");
                }
                return JsonError("失败");
            }
            return JsonError("失败");
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Delete(int id)
        {
            if (_service.Delete(id))
            {
                return JsonSuccess("删除成功！");
            }
            return JsonError("删除失败");
        }
        #endregion
    }
}